package com.hj.saapp.control.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.hj.saapp.BCDApplication;
import com.hj.saapp.R;
import com.hj.saapp.bean.MenuBean;
import com.hj.saapp.common.event.EventSetDevice;
import com.hj.saapp.common.manager.AppManager;
import com.hj.saapp.common.manager.UserManager;
import com.hj.saapp.common.utils.DialogUtil;
import com.hj.saapp.common.utils.DoubleClickExitHelper;
import com.hj.saapp.common.utils.HttpHelper;
import com.hj.saapp.control.activity.base.BaseActivity;
import com.hj.saapp.control.adapter.MainActivityAdapter;
import com.hj.saapp.control.adapter.MainActivityMenuAdapter;
import com.hj.saapp.control.listener.HJBDLocationListener;
import com.hj.saapp.control.listener.ListDialogListener;
import com.hj.saapp.view.GridViewWithHeaderAndFooter;
import com.postek.cdf.CDFPTKAndroidImpl;

import java.util.ArrayList;

import lib.hj.com.commonlib.bean.BaseBean;

import static com.hj.saapp.BCDApplication.cdf;
import static com.hj.saapp.R.array.item;

public class MainActivity extends BaseActivity implements DefaultDialog.OnDialogClickListener,AdapterView.OnItemClickListener{

    String PERMISSIONS_CONTACT[] = new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE,Manifest.permission.CAMERA};
    int REQUEST_CONTACTS = 1;
    private DoubleClickExitHelper mDoubleClickExit;
    GridViewWithHeaderAndFooter menuGv;
    MainActivityMenuAdapter menuGvAdapter;
    ArrayList<MenuBean> beans;
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new HJBDLocationListener();
    private DrawerLayout drawerLayout;
    private ListView listView;


//    int icons[] = {R.drawable.cj,R.drawable.ajcx,R.drawable.cwcx,R.drawable.bqbd,R.drawable.crk,R.drawable.pd,R.drawable.kwgz,R.drawable.kwgz,R.drawable.kwgz};
    int icons[] = {R.drawable.cj,R.drawable.ajcx,R.drawable.cwcx,R.drawable.bqbd};

    @Override
    public int getLayoutByid() {

        return R.layout.activity_app_main;
    }

    @Override
    public int getTitleById() {
        return R.string.main_title;
    }
    @Override
    public void initView() {
        initPermission();
        beans = new ArrayList<>();
        mDoubleClickExit = new DoubleClickExitHelper(this);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        listView = (ListView) findViewById(R.id.leftDrawerLv);


        String[] items = getResources().getStringArray(item);
        String[] title = null;
        String[] text = null;
        int icon[] = null;
        if (text == null || title == null) {

            text = getResources().getStringArray(R.array.menu);
            title = getResources().getStringArray(R.array.maintitle);
        }


        for (int i = 0; i < items.length; i++) {

            mData.add(items[i]);
        }
        for (int i = 0; i < title.length; i++) {

            MenuBean m = new MenuBean();
            m.text = text[i];
            m.title = title[i];
            m.id = icons[i];
            beans.add(m);

        }
        menuGv = (GridViewWithHeaderAndFooter) findViewById(R.id.menuGv);
        menuGvAdapter = new MainActivityMenuAdapter(beans);
        menuGv.setAdapter(menuGvAdapter);

        adapter = new MainActivityAdapter(mData);
        listView.setAdapter(adapter);

        menuGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        changeTo(MyUserInfoActivity.class);
                        break;
                    case 1:
                        changeTo(SaUserInfoActivityActivity.class);
                        break;
                    case 2:
                        changeTo(MyCaseSearchActivity.class);

                        break;
                    case 3:
                        changeTo(JieRuQu.class);
                                          break;
                    default:
                        showPopDialog("该功能暂不开放");
                        break;
                }


            }
        });
    }

    @Override
    public void initListener() {

        listView.setOnItemClickListener(this);

    }

    public void openDarw() {


        if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
            drawerLayout.closeDrawer(GravityCompat.END);
        } else {
            drawerLayout.openDrawer(GravityCompat.END);
        }

    }





    /**
     * 监听返回--是否退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 是否退出应用
            return mDoubleClickExit.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    public void initPermission() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED
                ||ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT,REQUEST_CONTACTS );

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        opration = position;
        openDarw();
        switch (opration) {
            case 0:
                changeTo(SetingActivity.class);
                break;
            case 1:
                changeTo(PrintSetingActivity.class);
                break;
            default:
                showPopDialog("该功能暂不开放");
                break;
        }

    }

    @Override
    public void getHttpData() throws Exception {
/**/
        switch (opration) {
            case 0:

                break;
            case 7:
                HttpHelper.getFinancialInfo(this);
                break;
            case 8:
                HttpHelper.getPropertyInfo(this);
                break;

            default:
                showPopDialog("暂没实现");
                baseView.showContent();
                break;
        }
    }

    @Override
    public void onSuccess(BaseBean paramObject) {
        super.onSuccess(paramObject);
        showPopDialog("请求成功");

    }

    @Override
    public void onFailure(int paramInt, String paramString) {

        String[] info = paramString.split(";;;;;;");
        showPopDialog("请求失败");
        baseView.showContent();
    }


    @Override
    public void initData() {
        if (cdf == null ) {
            cdf = new CDFPTKAndroidImpl(BCDApplication.getApp(), null);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            BCDApplication.cdf.PTK_DisconnectBluetooth();
        } catch (Exception e) {

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//
//        if (data != null) {
//
//            String filePath = data.getStringExtra("filePath");
//            if (!TextUtils.isEmpty(filePath)) {
//
//                if (filePath.endsWith(".xls")) {
//
//                    changeTo(ImportActivity.class,filePath);
//
//
//                } else {
//                    showPopDialog("请选择Excel文档");
//                }
//            }
//
//
//        }


    }
}
